package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Date;

import database.Database;


public class Appointment implements Comparable<Appointment> {

	/*
	 *  REGION private fields
	 */
	private String title;
	private Date date;
	private Time fromPointOfTime;
	private Time toPointOfTime;
	private String location;
	
	/*
	 *  REGION protected fields
	 */
	protected int ID;
	protected Person owner;
	
	/*
	 *  REGION public constructors
	 */
	public Appointment(int ID) {
		this.ID = ID;
		load();
	}
	
	public Appointment(String title, Date date, int fromPointOfTimeID, int toPointOfTimeID, String location, int ownerID) {
		setTitle(title);
		setDate(date);
		setFromPointOfTime(fromPointOfTimeID);
		setToPointOfTime(toPointOfTimeID);
		setLocation(location);
		setOwner(ownerID);
	}
	
	public Appointment(String title, Date date, int fromPointOfTime, int toPointOfTime, int OwnerId){
		setTitle(title);
		setDate(date);
		setFromPointOfTime(fromPointOfTime);
		setToPointOfTime(toPointOfTime);
		setOwner(OwnerId);
	}
	/*
	 *  REGION public getters
	 */
	public int getID() {
		return ID;
	}
	
	public String getTitle() {
		return title;
	}

	public Date getDate() {
		return date;
	}
	
	public Time getFromPointOfTime() {
		return fromPointOfTime;
	}
	
	public Time getToPointOfTime() {
		return toPointOfTime;
	}
	
	public String getLocation() {
		return location;
	}
	
	public Person getOwner() {
		return owner;
	}
	
	public int getOwnerID() {
		return owner.getID();
	}
	
	/*
	 * REGION public setters
	 */
	public void setID(int ID) {
		this.ID = ID;
	}
	
	public void setTitle(String title) {
		if (title.length() <= 50 && title != null) {
			this.title = title;
		}
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
	public void setFromPointOfTime(int timeID) {
		this.fromPointOfTime = new Time((timeID - 1) * 1000 * 60 * 60);
	}
	
	public void setToPointOfTime(int timeID) {
		this.toPointOfTime = new Time((timeID - 1) * 1000 * 60 * 60);
	}
	
	public void setFromPointOfTime(Time time) {
		this.fromPointOfTime = time;
	}
	
	public void setToPointOfTime(Time time) {
		this.toPointOfTime = time;
	}
	
	public void setLocation(String location) {
		if (location.length() <= 50 && location != null) { 
			this.location = location;
		}
	}
	
	public void setOwner(int ownerID) {
		this.owner = new Person(ownerID);
	}
	
	public void setOwner(Person owner) {
		this.owner = owner;
	}
	
	/*
	 * REGION public method
	 */
	public void load() {
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getAppointment(ID);
			
			if (rs.next()) {
				
				// Assign variables from the ResultSet to the object
				setTitle(rs.getString("title"));
				setDate(rs.getDate("date"));
				setFromPointOfTime(rs.getTime("fromPointOfTime"));
				setToPointOfTime(rs.getTime("toPointOfTime"));
				setLocation(rs.getString("location"));
				setOwner(new Person(rs.getInt("userID")));
			}
			
			else {
				
				System.out.println("Finner ingen bruker med ID" + ID);
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		database.disconnectDatabase();
	}
	
	public void Save() {
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		System.out.println(date.toString());
		
		if (ID == 0) {
			database.addAppointment(title, date.toString(), fromPointOfTime.toString(), toPointOfTime.toString(), location, owner.getID());
			ResultSet rs = database.appointmentGetIDFromEverything();
			try{
				if (rs.next()){
					setID(rs.getInt("ID"));
				}
				else{
					System.out.println("IDoppdatering gikk til helvete");
				}
			}catch(SQLException e){
					e.printStackTrace();
			}
		}
		else {
			database.updateAppointment(ID, title, date.toString(), fromPointOfTime.toString(), toPointOfTime.toString(), location, owner.getID());
		}

		
		database.disconnectDatabase();
	}
	
	public void Cancel() {
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		database.deleteAppointment(ID);
		
		database.disconnectDatabase();
	}
	
	/*
	 * REGION inherited methods
	 */
	public int compareTo(Appointment appointment) {
		
		if (this.date.compareTo(appointment.date) < 0) {
			return -1;
		}
		else if (this.date.compareTo(appointment.date) == 0) {
			if (this.getFromPointOfTime().compareTo(appointment.toPointOfTime) < 0) {
				return -1;
			}
			else if (this.fromPointOfTime.compareTo(appointment.fromPointOfTime) == 0) {
				return 0;
			}		
		}
		return 1;
	}
	
	public String toString() {
		return ID + ";" + title + ";" + date.toString() + ";" + fromPointOfTime + ";" + toPointOfTime + ";" + location + ";" + owner.getID();
	}
}